import React from 'react'
import { inject, observer } from 'mobx-react';
import { withRouter } from 'react-router-dom'
import { Form } from 'weaver-mobile-page'
import { Bubble, Tools, ActivityIndicator, Accordion } from 'weaver-mobile';
import ApprovalSettingsSelect from './ApprovalSettingsSelect';

const getLabel = Tools.getLabel;

@inject('wfFreeWorkflowSimple', 'wfGlobalStore', 'wfOperateStore')
@withRouter
@observer
export default class FreeWorkflowSimple extends React.Component {
    componentWillMount() {
        const { loadFormCondition } = this.props.wfFreeWorkflowSimple;
        const { baseParam = {} } = this.props.wfGlobalStore;
        loadFormCondition(baseParam);
    }


    render() {
        const { wfFreeWorkflowSimple } = this.props;
        const { animating } = wfFreeWorkflowSimple;
        return (<div className="wf-req-freeWorkflowSimple">
            <div className="wf-req-freeWorkflowSimple-title"><span>{getLabel(21781, '流转设定')}</span></div>
            {!animating && this.getFields()}
            {animating && <div className="wf-freeWorkflowSimple-loading">
                <ActivityIndicator
                    text="Loading..."
                    animating={animating}
                    size="large"
                />
            </div>}
        </div>)
    }

    getFields = () => {
        const { form, conditioninfo } = this.props.wfFreeWorkflowSimple;
        const { isInit } = form;
        const fields = [];
        if (isInit) {
            const formParams = form.formParams || {};
            conditioninfo.forEach((group) => {
                group.items.forEach((field) => {
                    if (formParams.isToNewAddSign && formParams.isToNewAddSign == 2 ) {
                        if( field.domkey[0] ==="isToNewAddSign"){
                            fields.push(<Form.Item
                                key={field.domkey.join('__')}
                                form={form}
                                fieldConfig={{...field}}
                                onChange={this.onChange}
                            />);
                        }
                    }else if(field.domkey[0] === 'approvalSettings') {
                            let {approvalSettings = ''} = formParams;
                            const valueArray = approvalSettings ? approvalSettings.split(',') : [];
                            fields.push(<Form.Item
                                key={field.domkey.join('__')}
                                form={form}
                                fieldConfig={{...field}}
                                children = {<ApprovalSettingsSelect
                                    {...field}
                                    onChange={v => this.onChange(field, (v || []).map(d => d && d.key))}
                                    valueArray={valueArray}
                                    {...field.otherParams}/>}
                            />);
                    }else{
                        fields.push(<Form.Item
                            key={field.domkey.join('__')}
                            form={form}
                            fieldConfig={{...field}}
                            onChange={this.onChange}
                        />);
                    }
                });
            });
        }
        return fields;
    }



    onChange = (condition, value) => {
        const { form, changeForm, chengeSubmitParams, setState } = this.props.wfFreeWorkflowSimple;
        form.onChange(condition, value);
        if (condition.domkey[0] == "isToNewAddSign") {
            changeForm(condition, value)
        } else {
            form.onChange(condition, value);
            let formParams = form.formParams;
            if(formParams.isToNewAddSign==1){
                setState({toNewFormData: form.formData})
            }
            let submitParams = {}
            if (condition.domkey[0] === "nodeOperator") {
                let ids = []
                value.map(item => {
                    ids.push(item.id)
                })
                submitParams.operatorids = ids.join(",");
            } else if (condition.domkey[0] === "countersignAttribute") {
                submitParams.countersignAttribute = value || "";
            } else if (condition.domkey[0] === "endTransfer") {
                submitParams.isFlowToNext = value || "";
            } else if (condition.domkey[0] === "approvalSettings") {
                submitParams.approvalSettings = value.join(',') || "";
            }
            chengeSubmitParams(submitParams);
        }
    }
}